package org.r505.chatroom.modules.user.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.websocket.Session;

import org.r505.chatroom.global.entity.Result;
import org.r505.chatroom.global.server.UserQueueFactory;
import org.r505.chatroom.modules.user.entity.User;

import com.alibaba.fastjson.JSON;

public class UserController {
	Map<String, Session> userQueue = UserQueueFactory.getInstance(); //获取用户队列
	/**
	 * 添加用户
	 * @param session
	 * @param param
	 */
	public void addUser(Session session, String param){
		Result<String> retVal = new Result<String>();
		User user = JSON.parseObject(param, User.class); //获取参数
		userQueue.put(user.getId(), session); //把用户加到队列
		retVal.setType("addUser");
		retVal.setData(user.getName());
		retVal.setStatus(1);
		
	}
	
	public void getUserList(Session session, String param) throws IOException{
		Result<List<String>> retVal = new Result<List<String>>();
		List<String> usernames = new ArrayList<String>();
		for(Entry<String, Session> entry : userQueue.entrySet()){
			usernames.add(entry.getKey());
		}
		retVal.setType("getUserList");
		retVal.setData(usernames);
		retVal.setStatus(1);
		for(Entry<String, Session> entry : userQueue.entrySet()){
			entry.getValue().getBasicRemote().sendText(JSON.toJSONString(retVal));
		}
	}
}
